<?php

require_once 'Ext/User/Edit/Password/Abstract.php';

class Ext_User_Edit_Password_Adapter_Ucenter extends Ext_User_Edit_Password_Abstract
{
    const NOTHING                 = 'nothing';
    const RECORD_NOT_FOUND        = 'RecordNotFound';
    const USER_IS_PROTECT         = 'userIsProtect';
    const UNCATEGORIZED           = 'uncategorized';
    protected $_messageTemplates = array(
        self::NOTHING                => "Nothing.",
        self::RECORD_NOT_FOUND       => "Record not found.",
        self::USER_IS_PROTECT        => "The user is protect.",
        self::UNCATEGORIZED          => "Uncategorized.",
    );
    
    public function edit($password)
    {
        $result = array(
            'code'     => Ext_User_Edit_Password_Result::FAILURE,
            'messages' => array(),
        );

        $userAccountBaseUsername = App_Model_DbTable_User_Account_Base_Username::getInstance()->getRowByUserId($this->_userId);
        if(!$userAccountBaseUsername)
        {
            $result['messages'][] = $this->_messager->createMessage(self::RECORD_NOT_FOUND);
            return new Ext_User_Edit_Password_Result($result['code'], $result['messages']);
        }
        $username = $userAccountBaseUsername['username'];
        if(!$username)
        {
            $result['messages'][] = $this->_messager->createMessage(self::RECORD_NOT_FOUND);
            return new Ext_User_Edit_Password_Result($result['code'], $result['messages']);
        }
        
        $ucenter = new Ext_Ucenter();
        $uid = $ucenter->changePassword($username, $password);

    	if($uid < 0) {
            if($uid == -7) {
                $result['messages'][] = $this->_messager->createMessage(self::NOTHING);
                
            } elseif($uid == -8) {
                $result['messages'][] = $this->_messager->createMessage(self::USER_IS_PROTECT);
            } else {
                $result['messages'][] = $this->_messager->createMessage(self::UNCATEGORIZED);
            }
            
            return new Ext_User_Edit_Password_Result($result['code'], $result['messages']);
        }
        
        $result['code'] = Ext_User_Edit_Password_Result::SUCCESS;
        return new Ext_User_Edit_Password_Result($result['code'], $result['messages']);
    }

}
